# _*_ coding utf-8 _*_
# george
# time: 2025/1/11下午4:54
# name: home.py
# comment:
from flask import Blueprint, render_template, session, redirect, request
from sqlalchemy import select, and_
from common.database import db_connection
from model.user import User
from model.file import File
import app.config.config
import logging
from app.settings import login_required

db_session, Base, engine = db_connection()

index_logger = logging.getLogger("index的controller的log:")

# 创建蓝图
index = Blueprint("index", __name__)


@index.route("/home")
@login_required
def get_home():
    # 获取当前页面的页数
    page = request.args.get("page")
    index_logger.debug(f"page:{page}")
    if page is None:
        page = 1
    # 获取的数据来自筛选
    if session.get("project_id") or session.get("team_id"):
        project_id = session.get("project_id")
        team_id = session.get("team_id")
        group_data = File().showfile_group(project_id, page) or File().showfile_team(team_id, page)
        session["project_id"] = False
        session["team_id"] = False
        return render_template("home.html", db_result=group_data, search_keyword=None,
                               user_info=session["user_info"])
    # 文件查询功能
    search_keyword = request.args.get("keyword")

    if search_keyword is not None:
        db_result = File().search_file(page, search_keyword)
    else:
        # 数据库查询数据,home page展示
        db_result = File().find_all_file(page)

    return render_template("home.html", db_result=db_result, search_keyword=search_keyword,
                           user_info=session["user_info"])


@index.route("/homeadd")
def add_data():
    File().add_file()
    return "pass"


